package com.photologics.main;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.photologics.logic.LogicHelper.coinGiveType;

public class HelpDialog extends Dialog implements OnClickListener {
	LinearLayout helpImageBoard;
	int[] imageId;
	int index;
	Context mcontext;
	String Preferences;
	int first;

	@SuppressWarnings("deprecation")
	public HelpDialog(Context context, String getPreferences) {
		super(context);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.help_dialog);
		getWindow().setLayout(android.view.ViewGroup.LayoutParams.FILL_PARENT,
				android.view.ViewGroup.LayoutParams.FILL_PARENT);
		Preferences = getPreferences;
		mcontext = context;
		first = 0;
		helpImageBoard = (LinearLayout) findViewById(R.id.help_image_board);
		helpImageBoard.setOnClickListener(this);

		index = 0;

		imageId = new int[18];
		imageId[0] = R.drawable.help_image0;
		imageId[1] = R.drawable.help_image1;
		imageId[2] = R.drawable.help_image2;
		imageId[3] = R.drawable.help_image3;
		imageId[4] = R.drawable.help_image4;
		imageId[5] = R.drawable.help_image5;
		imageId[6] = R.drawable.help_image6;
		imageId[7] = R.drawable.help_image7;
		imageId[8] = R.drawable.help_image8;
		imageId[9] = R.drawable.help_image9;
		imageId[10] = R.drawable.help_image10;
		imageId[11] = R.drawable.help_image11;
		imageId[12] = R.drawable.help_image12;
		imageId[13] = R.drawable.help_image13;
		imageId[14] = R.drawable.help_image14;
		imageId[15] = R.drawable.help_image15;
		imageId[16] = R.drawable.help_image16;
		imageId[17] = R.drawable.help_image17;

		if (!Preferences.equals("first")) {
			index = 1;
		}

		helpImageBoard.setBackgroundResource(imageId[index]);

	}

	@Override
	public void onClick(View v) {

		if (index != 17) {

			Drawable helpnextImage = helpImageBoard.getBackground();
			if (helpnextImage instanceof BitmapDrawable) {
				Bitmap b = ((BitmapDrawable) helpnextImage).getBitmap();
				b.recycle();
			}
			helpnextImage.setCallback(null);
			helpImageBoard.setBackgroundResource(imageId[++index]);

			if (index == 17)
				fistGetCoin(first);

		}

	}

	private void fistGetCoin(int first) {
		if (Preferences.equals("first")) {
			if (first == 0) {
				GiveCoinDialog giveCoinDialog = new GiveCoinDialog(mcontext,
						coinGiveType.FIRST);
				giveCoinDialog.getWindow().clearFlags(
						WindowManager.LayoutParams.FLAG_DIM_BEHIND);
				giveCoinDialog.setCanceledOnTouchOutside(false);
				giveCoinDialog.show();
				first = 1;
				final Handler handler = new Handler();
				handler.postDelayed(new Runnable() {
					@Override
					public void run() {
						dismiss();
					}
				}, 1500);
			}
		}
	}

	@Override
	public void onBackPressed() {
		Log.i("backkey", "ok");
		Intent intent = new Intent(mcontext, MainActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		mcontext.startActivity(intent);
		dismiss();
	}

}
